package quiz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class CreateQuizServlet
 */
@WebServlet("/CreateQuiz")
public class CreateQuizServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateQuizServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dpatch = request.getRequestDispatcher("index.jsp");
		dpatch.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Account acct = (Account) request.getSession().getAttribute("account");
		if (acct == null){
			request.setAttribute("error", "You're not logged in!  Please sign in to create a quiz.");
			RequestDispatcher dpatch = request.getRequestDispatcher("login.jsp");
			dpatch.forward(request, response);
			return;
		}


		ServletContext ctxt = request.getServletContext();
		DataBase db = (DataBase)ctxt.getAttribute("db");

		String name = (String)request.getParameter("name");
		if (name.equals("")){
			request.setAttribute("error", "Please give your quiz a name.");
			RequestDispatcher dpatch = request.getRequestDispatcher("create-quiz.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		String description = request.getParameter("description");
		if (description == null || description.trim().isEmpty()) description = "No description";
		boolean isOrdered = Boolean.parseBoolean((String)request.getParameter("ordered"));
		boolean isSinglePage = Boolean.parseBoolean((String)request.getParameter("single_page"));
		boolean hasPracticeMode = Boolean.parseBoolean((String)request.getParameter("practice"));
		boolean immediateCorrection = Boolean.parseBoolean((String)request.getParameter("correction"));
		boolean visibleToPublic = Boolean.parseBoolean((String)request.getParameter("visibility"));

		Quiz quiz = new Quiz(name, description, isOrdered, isSinglePage, hasPracticeMode, immediateCorrection, visibleToPublic, acct);
		
		db.storeQuiz(quiz);
		
		String tags = request.getParameter("tags");
		if (tags != null) {
			StringTokenizer st = new StringTokenizer(tags);
			
			while (st.hasMoreElements()) {
				quiz.setTag(st.nextToken());
			}
		}
		
		//CHECK FOR ACHIEVEMENTS HERE
		/*ArrayList<Quiz> allUserQuizzes = db.getQuizzesByUser(acct);
		if (allUserQuizzes.size() == 1){
			Achievement a = new Achievement(acct, Achievement.AMATEUR_AUTHOR);
			db.storeAchievement(a);
		} else if (allUserQuizzes.size() == 5){
			Achievement a = new Achievement(acct, Achievement.PROLIFIC_AUTHOR);
			db.storeAchievement(a);
		} else if (allUserQuizzes.size() == 10){
			Achievement a = new Achievement(acct, Achievement.PRODIGIOUS_AUTHOR);
			db.storeAchievement(a);
		}*/
		
		acct.checkAchievements();		
		response.sendRedirect("AddQuestion?id="+quiz.getID());
	}

}
